//
//  YJYVideoMaskView.m
//  BannerViewDemo
//
//  Created by BaizYi on 2019/1/8.
//  Copyright © 2019 BaizYi. All rights reserved.
//

#import "YJYVideoMaskView.h"

@interface YJYVideoMaskView ()

@property(strong,nonatomic) UIProgressView  *progressView;
@property(strong,nonatomic) UIButton  *startButton;
@property(strong,nonatomic) UITapGestureRecognizer  *singleTap;
@property(strong,nonatomic) UIButton  *replayButton;


@end

@implementation YJYVideoMaskView


-(void)playerCurrentTime:(NSInteger)currentTime totalTime:(NSInteger)totalTime sliderValue:(CGFloat)sliderValue{
    self.progressValue = sliderValue;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.progressView];
        [self addSubview:self.startButton];
        [self addSubview:self.replayButton];
        [self addGestureRecognizer:self.singleTap];
    }
    return self;
}

-(void)setProgressValue:(CGFloat)progressValue{
    _progressValue = progressValue;
    self.progressView.progress = progressValue;
    progressValue == 1 ? [self.replayButton setHidden:NO] : [self.replayButton setHidden:YES];
}

-(void)setIsStarButton:(BOOL)isStarButton{
    _isStarButton = isStarButton;
    isStarButton ? [self.startButton setHidden:YES] : [self.startButton setHidden:NO];
}

-(UITapGestureRecognizer *)singleTap{
    if (_singleTap == nil) {
        _singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
        _singleTap.numberOfTouchesRequired = 1;
        _singleTap.numberOfTapsRequired = 1;
    }
    return _singleTap;
}

-(void)singleTap:(UITapGestureRecognizer *)tap{
    if (self.progressValue == 1) {
        return;
    }
    if (self.buttonValue) {
        self.buttonValue(PlayerStateStart);
    }
}

-(UIProgressView *)progressView{
    if (_progressView == nil) {
        _progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 2, self.frame.size.width, 5)];
        _progressView.progressViewStyle = UIProgressViewStyleDefault;
        _progressView.progressTintColor = [UIColor orangeColor];
        _progressView.progress = 0;
    }
    return _progressView;
}
-(UIButton *)startButton{
    if (_startButton == nil) {
        _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _startButton.bounds = CGRectMake(0, 0, 40, 40);
        _startButton.center = self.center;
        [_startButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        [_startButton addTarget:self action:@selector(startButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startButton;
}
-(UIButton *)replayButton{
    if (_replayButton == nil) {
        _replayButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _replayButton.bounds = CGRectMake(0, 0, 50, 70);
        _replayButton.center = self.center;
        _replayButton.hidden = YES;
        [_replayButton setImage:[UIImage imageNamed:@"ZFPlayer_repeat_video"] forState:UIControlStateNormal];
        [_replayButton addTarget:self action:@selector(replayButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _replayButton;
}
-(void)startButtonAction{
    self.startButton.hidden = YES;
    if (self.buttonValue) {
        self.buttonValue(PlayerStateStart);
    }
}
-(void)replayButtonAction{
    self.replayButton.hidden = YES;
    if (self.buttonValue) {
        self.buttonValue(PlayerStateReplay);
    }
}
@end
